compname = getenv('computername');
if strcmp(compname,'DESKTOP-BHR0AU7')
    filedirLoad = 'E:\ANL\Experiments\RESULTS\VAST\EEG\';
    filedirSave = 'E:\ANL\Experiments\RESULTS\VAST\DATAforR\';
elseif strcmp(compname,'MSI')
    filedirLoad = 'E:\ANL\RESULTS\VAST\EEG\';
    filedirSave = 'E:\ANL\RESULTS\VAST\DATAforR\';
else
    error('Paths have not yet been set for this machine')
end

temp = load(strcat(filedirLoad, 'avg_eachERP.mat'));
avg = temp.avg;
clear temp

load('dummy_indSs.mat')
tb = dummy_indSs.time;
chans = dummy_indSs.label;
aud_cluster = {'Fz','AFz','FCz','F1','F2'};
vis_cluster = {'PO7', 'PO3', 'O1', 'PO8', 'PO4', 'O2'};
whereaud = contains(chans, aud_cluster);
wherevis = contains(chans, vis_cluster);

condList = avg(1,:);
max_posList = avg{2,1}(1,:); % INT ERPs incl here, but not in no INT conds
nTpts = size(avg{2,1}{2,1}, 2);
[erps_aud, erps_vis] = deal(nan(nTpts, length(condList), length(max_posList)));

for c = 1:length(condList)
    posct = 1;
    for pos = 1:length(max_posList)
        currpos = max_posList{pos};
        if any(contains(avg{2,c}(1,:), currpos))
            data_aud = avg{2,c}{2,posct}(whereaud,:);
            data_vis = avg{2,c}{2,posct}(wherevis,:);
            % average across channels in the cluster
            data_aud = mean(data_aud, 1);
            data_vis = mean(data_vis, 1);
            % populate simplified matrices
            erps_aud(:,c,pos) = data_aud;
            erps_vis(:,c,pos) = data_vis;
            posct = posct + 1;
        end
    end
end

% Save
save(strcat(filedirSave, "avg_eachERP.mat"), "tb", "condList",...
    "max_posList", "erps_aud", "erps_vis")
